﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Rio.Framework.Pages.Viewer;
using Rio.Framework.Pages.Helpers.Utils;
using System.Windows.Media.Imaging;
using System.IO;
using Rio.Framework.Pages.AppCode.Enums;
using Rio.Framework.Pages.Manager;
using Rio.Framework.Localisation;
using Rio.Framework.WCFService.RioWcfServices;
using Rio.Framework.WCFService;
using Rio.Applications.Web.Services;

namespace Rio.Framework.Pages.Controls
{
    public partial class PrintAreaControl : ControlBase, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private ObservableCollection<ImageRioSearch> m_ListItemSource;

        /// <summary>
        /// Gets or sets the list item source.
        /// </summary>
        /// <value>The list item source.</value>
        public ObservableCollection<ImageRioSearch> ListItemSource
        {
            get
            {
                return m_ListItemSource;
            }
            set
            {
                m_ListItemSource = value;
                OnPropertyChanged("ListItemSource");
            }
        }

        /// <summary>
        /// Gets or sets the container.
        /// </summary>
        /// <value>The container.</value>
        public UCViewer ParentContainer { get; set; }

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(null, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PrintAreaControl" /> class.
        /// </summary>
        public PrintAreaControl()
        {
            InitializeComponent();
            ListItemSource = new ObservableCollection<ImageRioSearch>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PrintAreaControl" /> class.
        /// </summary>
        /// <param name="container">The container.</param>
        public PrintAreaControl(UCViewer container)
            :this()
        {
            ParentContainer = container;
        }

        /// <summary>
        /// Handles the Click event of the Add control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void Add_Click(object sender, RoutedEventArgs e)
        {
            ImageRioSearch l_oImg = null;
            if (ParentContainer != null)
            {
                if (ParentContainer.m_oCurrentDocument != null)
                {
                    if (ParentContainer.CurrentDisplayMode == AppDisplayMode.Search)
                    {
                        l_oImg = new ImageRioSearch(ParentContainer.m_oCurrentDocument.Identifier,
                                        ParentContainer.m_oCurrentDocument.OriginalNameFile,
                                        ParentContainer.m_aRawImage,
                                        ParentContainer.m_aRawImage.Length,
                                        ParentContainer.m_oCurrentDocument.OriginalNameFile,
                                        ParentContainer.m_oCurrentDocument.ImageName,
                                        ParentContainer.m_oCurrentDocument.RelativePath);
                    }
                    else
                    {
                        l_oImg = new ImageRioSearch(0, "test", ParentContainer.m_aRawImage, ParentContainer.m_aRawImage.Length, "test", "test", "test");
                    }

                    l_oImg.IsFullSearch = false;
                    l_oImg.isDirectory = false;
                    l_oImg.IsCutedOrCopied = false;
                    l_oImg.NumberPages = 0;
                    ListItemSource.Add(l_oImg);
                }
            }
        }
        
        /// <summary>
        /// Handles the Click event of the Delete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < ListBoxVignette.SelectedItems.Count; i++)
            {
                ImageRioSearch item = (ImageRioSearch)ListBoxVignette.SelectedItems[i];

                ListItemSource.Remove(item);
            }
        }

        /// <summary>
        /// Handles the Click event of the Print control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void Print_Click(object sender, RoutedEventArgs e)
        {
            List<string> L_oFileToPrint = new List<string>();
            
            foreach (ImageRioSearch itemVignette in ListItemSource)
            {
                string l_sRelativeDocumentPath = itemVignette.ImageFullPath;
                L_oFileToPrint.Add(System.IO.Path.Combine(ParentContainer.ParameterStorage, l_sRelativeDocumentPath));
            }

            if (L_oFileToPrint != null && L_oFileToPrint.Count > 0)
            {
                ParentContainer.PrintDocumentsInPDFFormat(L_oFileToPrint);
                ParentContainer.DocumentPrintArea.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        /// <summary>
        /// Creates the thumbnail image.
        /// </summary>
        /// <param name="p_oImage">The p_o image.</param>
        /// <param name="width">The width.</param>
        /// <returns></returns>
        private Image CreateThumbnailImage(byte[] p_oImage, int width)
        {
            Image image;
            using (MemoryStream stream = new MemoryStream(p_oImage))
            {
                BitmapImage bi = new BitmapImage();
                bi.SetSource(stream);

                double cx = width;
                double cy = bi.PixelHeight * (cx / bi.PixelWidth);

                image = new Image();
                image.Width = cx;
                image.Height = cy;
                image.Source = bi;
                
            }
            return image;
        }


    }
}
